<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *果树管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择果树！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','image','remark','kucun','price','late_price','growtime','stage','classify_id','paixu','shoukg','shoufee','oneprice','createtime');
	$list=pdo_getslice('jy_farmer_guoshu',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['isnum']=pdo_count('jy_farmer_gdopt',array('uniacid'=>$_SESSION['uniacid'],'guoshu_id'=>$item['id']));
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'果树列表'),
			array('op'=>'edit','title'=>'添加果树'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'果树名称','type'=>'string'),
			array('name'=>'image','title'=>'果树图片','type'=>'image'),
			array('name'=>'growtime','title'=>'生长周期','type'=>'string'),
			array('name'=>'kucun','title'=>'库存','type'=>'string'),
			array('name'=>'isnum','title'=>'已认领','type'=>'string'),
			array('name'=>'price','title'=>'价格/棵','type'=>'string'),
			array('type'=>'unitstring','title'=>'预计经济收益','name'=>'shoukg','unit'=>'元/棵','help'=>''),
			array('type'=>'unitstring','title'=>'预计实物收获','name'=>'shoufee','unit'=>'KG/棵','help'=>''),
			array('type'=>'unitstring','title'=>'当前时价','name'=>'oneprice','unit'=>'元/KG','help'=>''),
			array('name'=>'status','title'=>'是否上架','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'果树名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'是否上架','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$return['status2']=array(array('id'=>0,'title'=>'下架'),array('id'=>1,'title'=>'上架'));
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_farmer_guoshu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['endtime']=$item['endtime']?:time()+20*24*3600;
	$item['endtime']=date('Y-m-d',$item['endtime']);
	$return['item']=$item;
	$return['item']['stage']=json_decode($return['item']['stage']);
	$return['item']['videos']=json_decode($return['item']['videos']);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回果树列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'果树名称','name'=>'title','help'=>''),
								array('type'=>'image','title'=>'果树图片','name'=>'image','help'=>''),
								array('type'=>'unitstring','title'=>'价格/棵','name'=>'price','unit'=>'元/棵','help'=>''),
								array('type'=>'unitstring','title'=>'认领起步数量','name'=>'minbuy','unit'=>'棵','help'=>''),
								//array('type'=>'string','title'=>'延迟价格/每日/棵','name'=>'late_price','help'=>''),
								array('type'=>'unitstring','title'=>'库存','name'=>'kucun','unit'=>'棵','help'=>''),
								array('type'=>'unitstring','title'=>'生长周期','name'=>'growtime','unit'=>'天','help'=>'有些果实是按周期来算的'),
								array('type'=>'radio','title'=>'预计收获月份','name'=>'shouyue','options'=>'months'),
								array('type'=>'unitstring','title'=>'预计经济收益','name'=>'shoufee','unit'=>'元/棵','help'=>''),
								array('type'=>'unitstring','title'=>'预计实物收获','name'=>'shoukg','unit'=>'KG/棵','help'=>''),
								array('type'=>'unitstring','title'=>'当前时价','name'=>'oneprice','unit'=>'元/KG','help'=>''),
								array('type'=>'unitstring','title'=>'回报经济收益','name'=>'shoufee2','unit'=>'元/棵','help'=>''),
								array('type'=>'unitstring','title'=>'回报实物收获','name'=>'shoukg2','unit'=>'KG/棵','help'=>''),
								array('type'=>'time','title'=>'截至时间','name'=>'endtime','help'=>''),
								array('name'=>'control_id','title'=>'监控设备','type'=>'select','options'=>'control'),
								array('name'=>'classify_id','title'=>'分类名称','type'=>'select','options'=>'classify','option_title'=>'title'),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'text','title'=>'果树简介','name'=>'remark','help'=>''),
								array('type'=>'text','title'=>'认领规则','name'=>'guize','help'=>''),
								array(
									'type'=>'array',
									'name'=>'videos',
									'title'=>'视频列表',
									'add_tip'=>'添加一个视频',
									'list'=>array(
										array('type'=>'string','name'=>'title','title'=>'视频名称'),	
										array('type'=>'image','name'=>'thumb','title'=>'视频LOGO'),
										array('type'=>'string','name'=>'video','title'=>'视频地址'),
									),
									'details'=>array(
										array('type'=>'string','name'=>'title','title'=>'视频名称'),	
										array('type'=>'image','name'=>'thumb','title'=>'视频LOGO'),
										array('type'=>'video','name'=>'video','title'=>'视频地址','help'=>'可以直接填远程网络地址，节省服务器压力，或系统设置远程附件（七牛，阿里云等，需要管理员操作！）'),	
									)
								),
								array('type'=>'richtext','title'=>'认领详情','name'=>'content','help'=>''),
								array(
									'type'=>'array',
									'name'=>'stage',
									'title'=>'生长阶段',
									'add_tip'=>'添加一个生长阶段',
									'list'=>array(
									array('type'=>'string','name'=>'stage_name','title'=>'阶段名称'),	
									array('type'=>'string','name'=>'stage_time','title'=>'周期'),	
									array('type'=>'image','name'=>'stage_image','title'=>'阶段图片'),
							
									),
									'details'=>array(
									array('type'=>'string','name'=>'stage_name','title'=>'阶段名称'),	
									array('type'=>'string','name'=>'stage_time','title'=>'生长周期','help'=>'仅填写数字，单位：天'),
									array('type'=>'image','name'=>'stage_image','title'=>'阶段图片'),	
							
									)
								),
							)
					),
					array(
						'title'=>'配送/自提设置',
							'list'=>array(
								array('type'=>'switch','title'=>'关闭快递配送？','name'=>'down_send_type1'),
								array('type'=>'switch','title'=>'关闭门店自取？','name'=>'down_send_type0'),
							)
					),			
			),
	);
	$return['months']=array(0=>'按生长周期',1=>'1月',2=>'2月',3=>'3月',4=>'4月',5=>'5月',6=>'6月',7=>'7月',8=>'8月',9=>'9月',10=>'10月',11=>'11月',12=>'12月');
	$return['classify']=pdo_getall('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	if($this->plugin_exists('jy_live')){
		$return['control']=pdo_getall('jy_live_list',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'price'=>intval($postdata['price']),
		'late_price'=>intval($postdata['late_price']),
		'kucun'=>intval($postdata['kucun']),
		'growtime'=>intval($postdata['growtime']),
		'stage'=>ijson_encode($postdata['stage']),
		'videos'=>ijson_encode($postdata['videos']),
		'control_id'=>intval($postdata['control_id']),
		'classify_id'=>intval($postdata['classify_id']),
		'remark'=>trim($postdata['remark']),
		'guize'=>trim($postdata['guize']),
		'content'=>trim($postdata['content']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'down_send_type1'=>intval($postdata['down_send_type1']),
		'down_send_type0'=>intval($postdata['down_send_type0']),
		'shoukg'=>trim($postdata['shoukg']),
		'shoufee'=>trim($postdata['shoufee']),
		'shoukg2'=>trim($postdata['shoukg2']),
		'shoufee2'=>trim($postdata['shoufee2']),
		'oneprice'=>trim($postdata['oneprice']),
		'endtime'=>strtotime($postdata['endtime']),
		'shouyue'=>intval($postdata['shouyue']),
		'minbuy'=>intval($postdata['minbuy']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_farmer_guoshu',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_farmer_guoshu',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_farmer_guoshu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的果树！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_farmer_guoshu',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
